package biz;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.util.List;

import dao.TClientInfo;

public class ClientInfoBiz {
	Configuration configuration = null;
	SessionFactory sessionFactory = null;
	Session session = null;
	Transaction transaction = null;
	
	public void AddClientInfo(TClientInfo sarv){
		ModClientInfo(sarv);
	}
	
	public void ModClientInfo(TClientInfo sarv){
		configuration = new Configuration().configure();
		DbComBiz dbbiz = new DbComBiz();
		String maxid = "";
		String qtype = "B";
		if(null == sarv.getId() || "".equals(sarv.getId().trim())){
			maxid = dbbiz.getMaxId(qtype);
			sarv.setId(qtype+maxid);
		}
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		session.saveOrUpdate(sarv);
		transaction.commit();
		dbbiz.updMaxId(qtype, Integer.parseInt(maxid));
		session.close();
	}
	
	public void DelClientInfo(TClientInfo sarv){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		TClientInfo tddmp = (TClientInfo)session.get(TClientInfo.class, sarv.getId());
		if(tddmp != null) {
			session.delete(tddmp);
		}
		transaction.commit();
		session.close();
	}
	
	public List GetClientInfo(String usertype){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TClientInfo as f where f.type = '" + usertype + "' order by f.id");
		
		List list =query.list();
		session.close();
		return list;
	}
	
	public Object GetClientInfoById(String oid){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TClientInfo as f where f.id ='" + oid + "' order by f.id");
		
		List list =query.list();
		session.close();
		
		if(list != null) {
			return list.get(0);
		}else{
			return null;
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
